import { create, update, remove,createRaw, query } from '../../../../../framework/service/index';
import { ToastTips } from '../../../../../common/toastTips/tosat';
import { routerRedux } from 'dva/router';

export default {

  namespace: 'myFeedback',

  state: {
    date: [],
    imgURL: [],
    particulars: '广州可圈点信息科技有限公司是以IT软件技术和管理不断创新为核心的客户需求导向型的信息技术公司，具有资深经历的IT信息化人才。能为广大用户提供专业、及时、周到的从IT规划、系统需求分析、产品免费体验、培训实施、技术服务、按需定制开发等全面的服务支持。以理解和满足客户的需求作为企业经营的首要任务，从用户的角度和发展的眼光来规划方案，与用户的业务与发展紧密结合在一起，切实解决企业管理中遇到的瓶颈和难题，从而为企业客户树立一个管理信息化、科学化，实施服务专业化的良好形象。',
    feedback: '',
    connectWay: '',
    uploadImgData:[],
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(location => {
        if(location.pathname === '/my-feedback') {
          // dispatch({
          //   type: 'query',
          // })
        }
      })
    },
  },



  effects: {
    *onSubmit({ payload }, { call, put, select }) {
      const { feedback, connectWay } = payload.data;
      const uploadImgData = yield select(({myFeedback}) => myFeedback.uploadImgData);
      let responseImgList = []
      if(uploadImgData && uploadImgData.length > 0){
        for (var i = 0; i < uploadImgData.length; i++) {
          // console.log(`uploadImgData[${i}].base64Url`, JSON.stringify(uploadImgData[i]));
          const { code, data, message } = yield call(createRaw, '/api/upload64?blur=false', uploadImgData[i].base64Url);
          if(code == 200){
            // for (var i = 0; i < uploadImgData.length; i++) {
              const imgUrl = {url:data.url};
              responseImgList.push(imgUrl);
            // }
          } else {
            ToastTips('上传图片失败', 2000, null);
            return;
          }
        }
      }
      const requestDeta = {
        content:feedback,
        connectWay,
        images:responseImgList,
      }
      // console.log('requestDeta', JSON.stringify(requestDeta));
      const { code, data, message } = yield call(create, '/api/feedback', {...requestDeta});
      if(code == 200){
        ToastTips('提交成功', 2000, null);
        yield put(routerRedux.goBack())
      }else{
        ToastTips('提交失败', 2000, null);
      }
    },

    *onUpload({ payload }, { call, put, select}) {
      const { imgURL, base64Url } = payload;
      const uploadImgData = yield select(({myFeedback}) => myFeedback.uploadImgData);

      let data = uploadImgData;
      if (data.length == 0) {
        data = [{
          id:1,
          imgURL,
          base64Url,
        }]
        yield put({
          type:'querySuccess',
          payload: {
            uploadImgData:data
          }
        })

      } else if(data.length < 3) {

        let maxId = -1;
        if(data.length == 1){
          maxId = data[0].id + 1;
        }else{
          let ids = [];
          data.map((item, index) =>{
            ids.push(item.id)
          })

          maxId = ids[0];
          for(var i= 0 ; i < ids.length; i++){
            if(maxId < ids[i]){
              maxId = ids[i];
            }
          }
          maxId = maxId + 1;
        }

        const newItem = {
          id:maxId,
          imgURL,
          base64Url,
        }
        data.push(newItem)

        yield put({
          type:'querySuccess',
          payload: {
            uploadImgData:data
          }
        })
      }

    },

    *onRemovePics({payload}, {call, put, select}) {

      const { imgId } = payload;
      const uploadImgData = yield select(({myFeedback}) => myFeedback.uploadImgData);

      const data = [];
      uploadImgData.map((item, index) => {
        if (item.id != imgId) {
          data.push(item)
        }
      })

      yield put({
        type: 'querySuccess',
        payload: {
          uploadImgData: data
        }
      })
    },

  },

  reducers: {
    querySuccess(state, action) {
      return { ...state, ...action.payload };
    },
  }

};
